<template>
  <common-list>
    <common-search slot="search" @search="handleQuery" @reset="resetQuery">
      <el-form
        :model="queryParams"
        ref="queryForm"
        @submit.native.prevent
        :inline="true"
        v-show="showSearch"
        label-width="80px"
      >
        <el-form-item label="业务系统" prop="applicationSystem">
          <el-select
            v-model="queryParams.applicationSystem"
            class="custom-select"
          >
            <el-option label="全部" :value="null"></el-option>
            <el-option
              class="custom-select"
              v-for="(item, index) in businessCodeList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictLabel"
            >
              <span class="selectList">
                <common-marquee-text :content="item.dictLabel">
                </common-marquee-text>
              </span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="数据库名称" prop="dbName">
          <common-input
            v-model="queryParams.dbName"
            clearable
            @enter="handleQuery"
          />
        </el-form-item>
      </el-form>
    </common-search>
    <el-row slot="tools" :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-switch
          v-model="isMatchNums"
          @change="matchNumChange"
          active-color="#13ce66"
          inactive-color="#909399"
          active-text="仅含有敏感字段数据"
          inactive-text="所有数据"
        >
        </el-switch>
      </el-col>
    </el-row>
    <div slot="tools" :gutter="10" class="mb8" style="margin-top: 15px">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-upload2"
          :loading="exportLoading"
          @click="handleExport"
          >导出
        </el-button>
      </el-col>
    </div>
    <common-table
      slot="content"
      v-loading="loading"
      :data="list"
      @sort-change="sortChange"
      :default-sort="defaultSort"
    >
      <el-table-column
        label="业务系统"
        align="left"
        prop="applicationSystem"
        fixed
        show-overflow-tooltip
      />
      <el-table-column
        label="数据库名称"
        align="left"
        prop="dbName"
        fixed
        show-overflow-tooltip
      />
      <el-table-column label="用户数量/库数量" align="left" prop="dbNum" />
      <el-table-column label="表数量" align="left" prop="tableNum" />
      <el-table-column
        label="敏感表数量"
        align="left"
        prop="senstableNum"
        sortable="custom"
      />
      <el-table-column label="字段数量" align="left" prop="columnNum" />
      <el-table-column
        label="敏感字段数量"
        align="left"
        prop="senscolumnNum"
        sortable="custom"
      />
      <el-table-column
        label="重要数据数量"
        align="left"
        prop="ext1"
        sortable="custom"
      />
      <el-table-column
        label="核心数据数量"
        align="left"
        prop="ext2"
        sortable="custom"
      />
    </common-table>

    <pagination
      slot="pagination"
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </common-list>
</template>

<script>
import { queryStatisticsListByPage } from "@/api/datascan/scanresults/statistics";
import BaseList from "@/mixins/BaseList";
import { getSingleArray, getFullDept } from "@/utils/util";
import { mapGetters } from "vuex";
import { queryBusinessList } from "@/api/datascan/dbinfo/info";
export default {
  name: "Statistics",
  mixins: [BaseList],
  components: {},
  data() {
    return {
      exportLoading: false,
      infoLoading: false,
      // 遮罩层
      loading: true,
      // 显示查询条件
      showSearch: true,
      // 总条数
      total: 0,
      // 扫描结果统计信息表格数据
      list: [],

      isHistoryDialog: false,
      historyList: [],
      historyTotal: 0,
      historyQueryParams: {
        dbId: null,
        pageNum: 1,
        pageSize: 10,
      },

      // 查询参数
      queryParams: {
        isPass: 1,
        pageNum: 1,
        pageSize: 10,
        dbName: null,
        deptIds: null,
        matchNums: "0", // 是否包含敏感字符  所有数据时为空字符串 仅包含敏感词时为 0
        feildName: "senstableNum",
        order: "desc",
        applicationSystem: null,
      },
      historyParams: {
        taskId: 101,
        historyId: 112,
        dbId: 101,
      },
      isReport: false,
      currentParent: 4,
      singleDeptList: [],

      isMatchNums: true,
      defaultSort: {
        prop: "senstableNum",
        order: "descending",
      },
      businessCodeList: [],
    };
  },
  computed: {
    ...mapGetters(["systemInfo"]),
  },
  created() {
    this.getList();
    this.queryBusinessCodeList();
  },
  methods: {
    //获取业务系统列表
    queryBusinessCodeList() {
      queryBusinessList().then((res) => {
        if (res && res.code === 200) {
          this.businessCodeList = res.data;
        } else {
          this.businessCodeList = [];
        }
      });
    },
    sortChange(sortInfo) {
      let { column, order, prop } = sortInfo;
      this.queryParams.order =
        order === "descending" ? "desc" : order === "ascending" ? "asc" : null;

      this.queryParams.feildName = this.queryParams.order ? prop : null;

      this.defaultSort.prop = this.queryParams.feildName;
      this.defaultSort.order = order;
      this.getList();
    },

    matchNumChange() {
      this.queryParams.matchNums = this.isMatchNums ? 0 : "";
      this.handleQuery();
    },
    getDeptName(val) {
      let dept = this.singleDeptList.find((item) => item.id === val.toString());
      if (dept) return getFullDept(dept.fulllabel);
    },
    //导出
    handleExport() {
      this.exportLoading = true;
      this.download(
        "datascan/b/scanresultstatistics/exportTemplate", //接口路径
        {
          ...this.queryParams,
          pageSize: 10000,
        },
        `扫描结果_库_${new Date().getTime()}.xlsx`
      ).finally(() => {
        this.exportLoading = false;
      });
    },
    /** 查询扫描结果统计信息列表 */
    getList() {
      this.loading = true;
      queryStatisticsListByPage(this.queryParams)
        .then((res) => {
          if (res && res.code === 200) {
            this.list = res.data.dataList;
            this.total = res.data.totalCount;
          } else {
            this.list = [];
            this.total = 0;
            this.handleListTips(res);
          }
        })
        .finally(() => {
          this.loading = false;
        });
    },
    /** 查询按钮操作 */
    handleQuery() {
      this.historyQueryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = this.$options.data().queryParams;
      // this.$refs.commonDept.clearDept();
    },
  },
};
</script>
<style lang="scss" scoped>
.el-dropdown-link {
  color: var(--theme-color);
}
</style>
